﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyPhongMach.View
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    public partial class Main : Window
    {
        #region PRIVATE VARIABLE
        private bool isMove = false;
        private string _title = "Phần mềm quản lý phòng mạch tư - ";
        private wLogin _login;
        private List<int> _history;
        #endregion

        #region PRIVATE FUNCTION
        //Hàm lấy tiêu đề cho form
        private string getTitle(int id)
        {
            switch (id)
            {
                case 0: return "Quản lý";
                case 1: return "Nghiệp vụ";
                case 2: return "Hệ thống";
                case 3: return "Tài khoản";
                default: return "Phần mềm";
            }
        }
        //Hàm xử lý hiển thị cho phần Quản lý
        private void quanlyVisibility(int id)
        {
            switch (id)
            {
                case 1:
                    QLBenhNhan.Visibility = QLCachDung.Visibility = QLDonVi.Visibility = QLThuoc.Visibility = Visibility.Collapsed;
                    QLBenh.Visibility = Visibility.Visible;
                    break;
                case 2:
                    QLBenhNhan.Visibility = QLBenh.Visibility = QLDonVi.Visibility = QLCachDung.Visibility = Visibility.Collapsed;
                    QLThuoc.Visibility = Visibility.Visible;
                    break;
                case 3:
                    QLBenhNhan.Visibility = QLBenh.Visibility = QLDonVi.Visibility = QLThuoc.Visibility = Visibility.Collapsed;
                    QLCachDung.Visibility = Visibility.Visible;
                    break;
                case 4:
                    QLBenhNhan.Visibility = QLBenh.Visibility = QLCachDung.Visibility = QLThuoc.Visibility = Visibility.Collapsed;
                    QLDonVi.Visibility = Visibility.Visible;
                    break;
                default:
                    QLDonVi.Visibility = QLBenh.Visibility = QLCachDung.Visibility = QLThuoc.Visibility = Visibility.Collapsed;
                    QLBenhNhan.Visibility = Visibility.Visible;
                    break;
            }
        }
        //Hàm xử lí hiển thị cho phần Nghiệp vụ
        private void nghiepvuVisibility(int id)
        {
            switch (id)
            {
                case 1:
                    NVDoanhSo.Visibility = NVHoaDon.Visibility = NVPhieuKham.Visibility = NVQuyDinh.Visibility = NVSuDung.Visibility = NVTraCuu.Visibility = Visibility.Collapsed;
                    NVBenhNhan.Visibility = Visibility.Visible;
                    break;
                case 2:
                    NVBenhNhan.Visibility = NVDoanhSo.Visibility = NVHoaDon.Visibility = NVPhieuKham.Visibility = NVQuyDinh.Visibility = NVSuDung.Visibility = Visibility.Collapsed;
                    NVTraCuu.Visibility = Visibility.Visible;
                    break;
                case 3:
                    NVTraCuu.Visibility = NVBenhNhan.Visibility = NVDoanhSo.Visibility = NVHoaDon.Visibility = NVQuyDinh.Visibility = NVSuDung.Visibility = Visibility.Collapsed;
                    NVPhieuKham.Visibility = Visibility.Visible;
                    break;
                case 4:
                    NVPhieuKham.Visibility = NVTraCuu.Visibility = NVBenhNhan.Visibility = NVDoanhSo.Visibility = NVQuyDinh.Visibility = NVSuDung.Visibility = Visibility.Collapsed;
                    NVHoaDon.Visibility = Visibility.Visible;
                    break;
                case 5:
                    NVHoaDon.Visibility = NVPhieuKham.Visibility = NVTraCuu.Visibility = NVBenhNhan.Visibility = NVQuyDinh.Visibility = NVSuDung.Visibility = Visibility.Collapsed;
                    NVDoanhSo.Visibility = Visibility.Visible;
                    break;
                case 6:
                    NVDoanhSo.Visibility = NVHoaDon.Visibility = NVPhieuKham.Visibility = NVTraCuu.Visibility = NVBenhNhan.Visibility = NVQuyDinh.Visibility = Visibility.Collapsed;
                    NVSuDung.Visibility = Visibility.Visible;
                    break;
                default:
                    NVSuDung.Visibility = NVDoanhSo.Visibility = NVHoaDon.Visibility = NVPhieuKham.Visibility = NVTraCuu.Visibility = NVBenhNhan.Visibility = Visibility.Collapsed;
                    NVQuyDinh.Visibility = Visibility.Visible;
                    break;
            }
        }
        //Hàm xử lí cho phần Hệ thống
        private void hethongVisibility(int id)
        {
            switch (id)
            {
                case 1:
                    HTKhoiPhuc.Visibility = HTSaoLuu.Visibility = Visibility.Collapsed;
                    HTCauHinh.Visibility = Visibility.Visible;
                    break;
                case 2:
                    HTCauHinh.Visibility = HTKhoiPhuc.Visibility = Visibility.Collapsed;
                    HTSaoLuu.Visibility = Visibility.Visible;
                    break;
                default:
                    HTCauHinh.Visibility = HTSaoLuu.Visibility = Visibility.Collapsed;
                    HTKhoiPhuc.Visibility = Visibility.Visible;
                    break;
            }
        }
        //Hàm xử lí hiển thị cho phần Tài khoản
        private void taikhoanVisibility(int id)
        {
            switch (id)
            {
                case 1:
                    TKQuanLy.Visibility = Visibility.Collapsed;
                    TKThongTin.Visibility = Visibility.Visible;
                    break;
                default:
                    TKQuanLy.Visibility = Visibility.Visible;
                    TKThongTin.Visibility = Visibility.Collapsed;
                    break;
            }
        }
        //Hàm xử lí hiển thị cho phần Phần mềm
        private void phanmemVisibility(int id)
        {
            switch (id)
            {
                case 1:
                    PMGioiThieu.Visibility = Visibility.Collapsed;
                    PMHDSuDung.Visibility = Visibility.Visible;
                    break;
                default:
                    PMHDSuDung.Visibility = Visibility.Collapsed;
                    PMGioiThieu.Visibility = Visibility.Visible;
                    break;
            }
        }
        //Hàm chèn page vào các tabItem
        private void addPage()
        {
            Page page;
            Frame frm;
            frm = new Frame();
            page = new WBenh();
            frm.Navigate(page);
            QLBenh.Children.Add(frm);
            //Quản lý thuốc
            frm = new Frame();
            page = new WThuoc();
            frm.Navigate(page);
            QLThuoc.Children.Add(frm);
            //Quản lý Cách dùng
            frm = new Frame();
            page = new WCachDung();
            frm.Navigate(page);
            QLCachDung.Children.Add(frm);
            //Quản lý Đơn vị
            frm = new Frame();
            page = new WDonVi();
            frm.Navigate(page);
            QLDonVi.Children.Add(frm);
            //Quản lý bệnh nhân
            frm = new Frame();
            page = new WBenhNhan();
            frm.Navigate(page);
            QLBenhNhan.Children.Add(frm);

            //Nghiệp vụ khám bệnh
            frm = new Frame();
            page = new WDanhSachBenhNhan();
            frm.Navigate(page);
            NVBenhNhan.Children.Add(frm);

            //Tra cứu
            frm = new Frame();
            page = new WTraCuuBenhNhan();
            frm.Navigate(page);
            NVTraCuu.Children.Add(frm);

            //Nghiệp vụ Phiếu Khám bệnh
            frm = new Frame();
            page = new WKhamBenh();
            frm.Navigate(page);
            NVPhieuKham.Children.Add(frm);

            //Đổi quy định
            frm = new Frame();
            page = new WThamSo();
            frm.Navigate(page);
            NVQuyDinh.Children.Add(frm);


        }
        #endregion

        #region CONSTRUCTOR
        public Main(wLogin parent)
        {
            InitializeComponent();
            this._login = parent;
            _history = new List<int>();
        }

        public void TaoLienKet()
        {
            addPage();
        }
        #endregion

        #region EVENT
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {

        }

        private void TabMenu_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            string tmp = getTitle(TabMenu.SelectedIndex);
            this.Title = _title + tmp;
            if (lbLichSu.Items.IndexOf(tmp) < 0)
                lbLichSu.Items.Add(tmp);
            _history.Add(TabMenu.SelectedIndex);
            _history = _history.Distinct().ToList();
        }
        private void smBenh_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            quanlyVisibility(1);
        }

        private void smThuoc_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            quanlyVisibility(2);
        }

        private void smCachDung_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            quanlyVisibility(3);
        }

        private void smDonVi_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            quanlyVisibility(4);
        }

        private void smBenhNhan_MouseLeftButtonDown_2(object sender, MouseButtonEventArgs e)
        {
            quanlyVisibility(5);
        }

        private void smDSBenhNhan_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(1);
        }

        private void smTraCuu_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(2);
        }

        private void smPhieuKham_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(3);
        }

        private void smHoaDon_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(4);
        }

        private void smDoanhSo_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(5);
        }

        private void smSuDung_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(6);
        }

        private void smQuyDinh_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            nghiepvuVisibility(7);
        }

        private void smCauHinh_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            hethongVisibility(1);
        }

        private void smSaoLuu_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            hethongVisibility(2);
        }

        private void smKhoiPhuc_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            hethongVisibility(3);
        }

        private void smTTTaiKhoan_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            taikhoanVisibility(1);
        }

        private void smQLTaiKhoan_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            taikhoanVisibility(2);
        }

        //Xử lý đăng xuất
        private void smDangXuat_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            this._login.Show();
            this.Close();
        }

        private void smHDSuDung_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            phanmemVisibility(1);
        }

        private void smGioiThieu_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            phanmemVisibility(2);
        }

        private void lbLichSu_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            TabMenu.SelectedIndex = _history[lbLichSu.SelectedIndex];
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
            this._login.Show();
        }

        private void Grid_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            isMove = true;
        }

        private void Grid_MouseMove_1(object sender, MouseEventArgs e)
        {
            try
            {
                if (isMove) this.DragMove();
            }
            catch { }
        }

        private void Grid_MouseUp_1(object sender, MouseButtonEventArgs e)
        {
            isMove = false;
        }
        #endregion

    }
}
